package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.annotation.WebFilter;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public Result save(@RequestBody SetmealDto setmealDto){

        setmealService.saveWithDish(setmealDto);

        return Result.success("添加成功");
    }

    @GetMapping("/page")
    public Result getByPage(Integer page ,Integer pageSize,String name){

        Page<Setmeal> setmealPage = new Page<>();

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();

        wrapper.like(name!=null,Setmeal::getName,name);

        setmealPage =setmealService.page(setmealPage,wrapper);

        Page<SetmealDto> setmealDishPage = new Page<>();

        setmealDishPage = mapperFacade.map(setmealPage, setmealDishPage.getClass());

        List<Setmeal> records = setmealPage.getRecords();

        List<SetmealDto> setmealDtos = records.stream().map(item -> {
            SetmealDto setmealDto = mapperFacade.map(item, SetmealDto.class);
            Long categoryId = item.getCategoryId();
            Category id = categoryService.getById(categoryId);
            if (id != null) {
                setmealDto.setCategoryName(id.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());

        setmealDishPage.setRecords(setmealDtos);

        return Result.success(setmealDishPage);
    }


    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){

        setmealService.removeWithDish(ids);

        return Result.success("删除成功");
    }


    @PostMapping("/status/{sta}")
    public Result updateStatus(@PathVariable("sta") String sta,@RequestParam(name = "ids") List<Long> ids){

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();

        wrapper.in(Setmeal::getId,ids);

        List<Setmeal> list = setmealService.list(wrapper);

        list  = list.stream().map(item -> {
            item.setStatus(Integer.parseInt(sta));
            return item;
        }).collect(Collectors.toList());

        setmealService.updateBatchById(list);

        return  Result.success("修改成功");

    }



    @GetMapping("/list")
    public Result list(@RequestBody Setmeal setmeal){

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());

        wrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());

        wrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(wrapper);

        return Result.success(list);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){

        SetmealDto setmealDto=setmealService.getByIdWithDish(id);

        return Result.success(setmealDto);

    }

    @PutMapping
    public Result update(@RequestBody SetmealDto setmealDto){

        setmealService.updateWithDish(setmealDto);

        return Result.success("修改成功");

    }


}
